package oving5.person;

import java.util.ArrayList;


public class Person {
	private String name;
	private char gender;
	private ArrayList<Person> children;
	private Person mother;
	private Person father;
	
	public Person(char gender){
		if(gender=='m'||gender=='f'){
			this.gender =gender; 
		}
		else{
			this.gender='q';
		}
		children=new ArrayList<Person>();
		
		
	}
	public boolean isMale(){
		if(gender=='m'){
			return true;
		}
		return false;
	}
	public boolean isFemale(){
		if(gender=='f'){
			return true;
		}
		return false;
		
	}
	public String getName(){
		return name;
	}
	public void setName(String name){
		if(name==null){
			this.name=null;
		}
		else{
			boolean ok=true;
			for(int a=0;a<name.length();a++){
				if(!(Character.isLetter(name.charAt(a))||name.charAt(a)==' ')){
					ok=false;
					break; 
				}
			}
			if(ok){
				this.name=name;
					
			}
		}			
	}
	public int getChildCount(){
		return children.size();
	}
	public Person getMother(){
		return mother;
	}
	public void setMother(Person mother){
		if(mother!=this.mother){
			if(mother==null){
				if(this.mother!=null){
					this.mother.removeChild(this);
				}
			}
			else{
				if(mother.isFemale()){
					if(this.mother!=null){
						this.mother.removeChild(this);
					}
					if(!mother.children.contains(this)){
						mother.addChild(this);
					}
				
					this.mother=mother;
				}
			}
		}
	}
	public Person getFather(){
		return father;
	}
	public void setFather(Person father){
		if(father!=this.father){
			if(father==null){
				if(this.father!=null){
					this.father.removeChild(this);
				}
			}
			else{
				if(father.isMale()){
					if(this.father!=null){
						this.father.removeChild(this);
					}
					if(!father.children.contains(this)){
						father.addChild(this);
					}
					this.father=father;
					
				}
			}
		}
	}
	public Person getChild(int index){
		return children.get(index);
	}
	public void addChild(Person child){
		
		if(!this.children.contains(child)){
			this.children.add(child);
			if(this.isMale()){
				child.setFather(this);
			}
			if(this.isFemale()){
				child.setMother(this);
			}
		}
		
	}
	public void removeChild(Person child){
		if(this.children.contains(child)){
			if(this==child.father){
				child.father=null;
			}
			if(this==child.mother){
				child.mother=null;
			}
			this.children.remove(child);

		}

		
	}
	public int indexOfChild(Person p){
		if(containsChild(p)){
			return this.children.indexOf(p);
		}
		else{
			return -1;
		}
	}
	public boolean containsChild(Person p){
		return this.children.contains(p);
	}
	public boolean isAncestorOf(Person p){
		boolean isAncestor=false;
		if(this.children.contains(p)){
			isAncestor=true;
		}
		for(int i=0;i<this.getChildCount();i++){
			if(this.getChild(i).isAncestorOf(p)){
				isAncestor=true;
				break;
			}
		}
		return isAncestor;
	}
	public static void main(String[] args){
		

	}
}
